use actix_web::HttpResponse;
use serde::Serialize;

#[derive(Serialize)]
pub struct ApiResponse<T: Serialize> {
    pub code: usize,
    pub msg: String,
    pub data: Option<T>,
}

impl<T: Serialize> ApiResponse<T> {
    pub(crate) fn success(data: T) -> HttpResponse {
        HttpResponse::Ok().json(ApiResponse {
            code: 0,
            msg: String::new(),
            data: Some(data),
        })
    }

    pub(crate) fn fail(code: usize, msg: &str) -> HttpResponse {
        // 指定返回类型为 ApiResponse<()>
        let response: ApiResponse<()> = ApiResponse {
            code,
            msg: msg.to_string(),
            data: None,
        };
        HttpResponse::Ok().json(response)
    }
}
